package com.kuang.demo.service.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kuang.demo.bo.wx.WxDeleteAddressBO;
import com.kuang.demo.bo.wx.WxSaveDetailAddressBO;
import com.kuang.demo.mapper.RegionMapper;
import com.kuang.demo.mapper.wx.WXUserMapper;
import com.kuang.demo.mapper.wx.WxAddressMapper;
import com.kuang.demo.pojo.wx.Cskaoyanmall_address;
import com.kuang.demo.vo.wx.WxQueryDetailAddressVO;
import org.apache.ibatis.annotations.Update;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 郭浩 on 2020/11/5 11:09
 */
@Service
public class WxAddressServiceImpl implements WxAddressService{
    @Autowired
    WxAddressMapper wxAddressMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    WXUserMapper wxUserMapper;
    @Override
    public WxQueryDetailAddressVO queryDetailAddress(int id) {
        //查汇总信息
        QueryWrapper<Cskaoyanmall_address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("id",id);
        Cskaoyanmall_address cskaoyanmall_address = wxAddressMapper.selectOne(addressQueryWrapper);
        //查城市名字
        String province = regionMapper.queryNameById(cskaoyanmall_address.getProvinceId());
        String city = regionMapper.queryNameById(cskaoyanmall_address.getCityId());
        String area = regionMapper.queryNameById(cskaoyanmall_address.getAreaId());

        WxQueryDetailAddressVO wxQueryDetailAddressVO = new WxQueryDetailAddressVO();
        wxQueryDetailAddressVO.setIsDefault(cskaoyanmall_address.getIsDefault());
        wxQueryDetailAddressVO.setAreaId(cskaoyanmall_address.getAreaId());
        wxQueryDetailAddressVO.setAddress(cskaoyanmall_address.getAddress());
        wxQueryDetailAddressVO.setCityName(city);
        wxQueryDetailAddressVO.setAreaName(area);
        wxQueryDetailAddressVO.setName(cskaoyanmall_address.getName());
        wxQueryDetailAddressVO.setMobile(cskaoyanmall_address.getMobile());
        wxQueryDetailAddressVO.setId(cskaoyanmall_address.getId());
        wxQueryDetailAddressVO.setCityId(cskaoyanmall_address.getCityId());
        wxQueryDetailAddressVO.setProvinceName(province);
        wxQueryDetailAddressVO.setProvinceId(cskaoyanmall_address.getProvinceId());
        return wxQueryDetailAddressVO;
    }

    @Override
    public void updateDetailAddress(WxSaveDetailAddressBO wxSaveDetailAddressBO) {
        //查看id 如果是0就是新增 如果不是0就是更行
        Cskaoyanmall_address cskaoyanmall_address = new Cskaoyanmall_address();
        if (wxSaveDetailAddressBO.getId() != 0){
            cskaoyanmall_address.setAddress(wxSaveDetailAddressBO.getAddress());
            cskaoyanmall_address.setAreaId(wxSaveDetailAddressBO.getAreaId());
            cskaoyanmall_address.setCityId(wxSaveDetailAddressBO.getCityId());
            cskaoyanmall_address.setId(wxSaveDetailAddressBO.getId());
            cskaoyanmall_address.setIsDefault(wxSaveDetailAddressBO.isIsDefault());
            cskaoyanmall_address.setMobile(wxSaveDetailAddressBO.getMobile());
            cskaoyanmall_address.setName(wxSaveDetailAddressBO.getName());
            cskaoyanmall_address.setProvinceId(wxSaveDetailAddressBO.getProvinceId());

            //查询id对应的userid
//            QueryWrapper<Cskaoyanmall_address> cskaoyanmall_addressQueryWrapper = new QueryWrapper<>();
//            cskaoyanmall_addressQueryWrapper.eq("id",wxSaveDetailAddressBO.getId());
//            Cskaoyanmall_address cskaoyanmall_address1 = wxAddressMapper.selectOne(cskaoyanmall_addressQueryWrapper);
//            Integer userId = cskaoyanmall_address1.getUserId();
            Subject subject = SecurityUtils.getSubject();
            String username = (String) subject.getPrincipal();
            Integer userId = wxUserMapper.selectUserIdByName(username);
            //将userId等于这个人，同时更新的那个id不变，将default置为0
            wxAddressMapper.updateById(cskaoyanmall_address);
            UpdateWrapper<Cskaoyanmall_address> cskaoyanmall_addressUpdateWrapper = new UpdateWrapper<>();
            cskaoyanmall_addressUpdateWrapper
                    .eq("user_id",userId)
                    .ne("id",wxSaveDetailAddressBO.getId());
            Cskaoyanmall_address cskaoyanmall_address1 = new Cskaoyanmall_address();
            cskaoyanmall_address1.setIsDefault(false);
            wxAddressMapper.update(cskaoyanmall_address1,cskaoyanmall_addressUpdateWrapper);
        }else {
            //新增要先通过username找到userid
            Subject subject = SecurityUtils.getSubject();
            String username = (String) subject.getPrincipal();
            int i = wxUserMapper.selectUserIdByName(username);
            Integer userId = i;

            wxAddressMapper.updateById(cskaoyanmall_address);
            UpdateWrapper<Cskaoyanmall_address> cskaoyanmall_addressUpdateWrapper = new UpdateWrapper<>();
            cskaoyanmall_addressUpdateWrapper
                    .eq("user_id",userId);
            Cskaoyanmall_address cskaoyanmall_address1 = new Cskaoyanmall_address();
            wxAddressMapper.update(cskaoyanmall_address1,cskaoyanmall_addressUpdateWrapper);
            //使用userid绑定到同i个用户
            cskaoyanmall_address.setUserId(userId);
            cskaoyanmall_address.setAddress(wxSaveDetailAddressBO.getAddress());
            cskaoyanmall_address.setAreaId(wxSaveDetailAddressBO.getAreaId());
            cskaoyanmall_address.setCityId(wxSaveDetailAddressBO.getCityId());
            cskaoyanmall_address.setId(wxSaveDetailAddressBO.getId());
            cskaoyanmall_address.setIsDefault(wxSaveDetailAddressBO.isIsDefault());
            cskaoyanmall_address.setMobile(wxSaveDetailAddressBO.getMobile());
            cskaoyanmall_address.setName(wxSaveDetailAddressBO.getName());
            cskaoyanmall_address.setProvinceId(wxSaveDetailAddressBO.getProvinceId());
            //将新增之后的id
        }
    }

    @Override
    public void deleteDetailAddress(WxDeleteAddressBO wxDeleteAddressBO) {
        Cskaoyanmall_address cskaoyanmall_address = new Cskaoyanmall_address();
        cskaoyanmall_address.setId(wxDeleteAddressBO.getId());
        cskaoyanmall_address.setDeleted(1);
        wxAddressMapper.updateById(cskaoyanmall_address);
    }


}
